gitPath='/home/apps_admin/sources/WFAPICORE';
buildsPath='/home/apps_admin/wfapi_builds';

version="v1."$(date +%y%m%d);

cd $gitPath;
source /etc/profile &> /dev/null;

port=7020;
branch='main';
env='Development';
deplyType='--uat';

# sis
function sit() {
  if [ "${1,,}" = "" ]; then
    # git switch branch and pull codes.
    git checkout . &> /dev/null;
    
    if git checkout $branch &> /dev/null; then
      echo "  switch to branch:${branch}...";
      git checkout -b $branch origin/$branch &> /dev/null;
      echo '  branch switch completed!';
    fi
    
    echo '  git pull...';
    git pull;
    echo '  git pull completed!';
    
    # netcore publish
    echo '  app build&publish...';
    rm -rf $buildsPath/* &> /dev/null;
    cd $gitPath/WFApiCore;
    dotnet publish -c release -o $buildsPath &> /dev/null;
    
    if [ $? -ne 0 ]; then
      echo "  app build&publish error, please retry!";
    
      exit 1;
    fi
    
    chmod 777 -R $buildsPath;
    echo '  app build&publish completed!';
  else
    cp $buildsPath/ConfigsProduction/* $buildsPath/Configs &> /dev/null;
  fi
  
  echo '  remove docker app and image...';
  docker rm -f `docker ps -a | grep $port | awk {'print $1'}` &> /dev/null;
  docker rmi -f wfapi_${branch,,}_$port &> /dev/null;
  echo '  remove docker app and image completed!';
  
  echo '  build docker image...';
  
  # docker builder and start app.
  cd $buildsPath;
  docker build -t wfapi_${branch,,}_$port . &> /dev/null;
  
  if [ $? -ne 0 ]; then
    echo "  docker image build error, please retry!";
  
    exit 1;
  fi
  
  echo '  build docker image completed!';
  echo "  docker app is runing: wfapi_${branch,,}_$port";
  docker run -e "ASPNETCORE_ENVIRONMENT=${env}" --restart=always --name=wfapi_${branch,,}_$port -p $port:80 -v /mnt/iWorkflowUploadFiles:/app/wwwroot/files -d wfapi_${branch,,}_$port &> /dev/null;
  
  if [ $? -eq 0 ]; then
    echo "update success, please visit: http://10.164.2.213:${port}/swagger/index.html";
  fi  
}

if [ $# -ge 1 ]; then
  case "${1,,}" in
    mar) branch='2022_Mar'; port=7021;;
    *) branch='main'; port=7020;;
  esac

  if [[ "${1,,}" != "--"* ]]; then
    shift;
  fi
fi

if [[ "${1,,}" = "--"* ]]; then
  deplyType="${1,,}";
  
  shift;
  
  if [[ "${1,,}" = "v1."* ]]; then
    fullVersion="$1";
    
    shift;
  else
    if [ "$deplyType" = "--prd" ]; then
      sit "--prd";
    fi
    
    counts=$(docker images | grep "10.164.2.213:4300/wfapi/api" | grep "$version" | awk '{print $3}' | wc -l);
    
    newImageId=$(docker images | grep wfapi_${branch,,}_${port} | awk '{print $3}');
    oldImageId=$(docker images | grep 10.164.2.213:4300/wfapi/api | grep $version.$counts | awk '{print $3}');
    
    fullVersion="$version.$counts";
    
    if [ "$newImageId" != "$oldImageId" ]; then
      counts=$[$counts + 1];
      fullVersion="$version.$counts";
      
      echo '  begin pull...';
      
      docker tag wfapi_${branch,,}_${port} 10.164.2.213:4300/wfapi/api:$fullVersion &> /dev/null;
      docker push 10.164.2.213:4300/wfapi/api:$fullVersion;
      
      echo "  pull completed, the version is:${fullVersion}";
    fi
  fi
  
  if [ -n "$deplyType" ]; then
    case $deplyType in
      --uat)
        echo "  begin deploy to uat...";
        sshpass -p 'C@pitaland123' ssh apps_admin@10.164.2.133 "sudo sh deploy_api.sh ${fullVersion} $*";;
      --prd)
        echo "  begin deploy to production...";
        sshpass -p 'C@pitaland123' ssh apps_admin@10.164.5.160 "sudo sh deploy_api.sh ${fullVersion} $*";
        
        sit;;
      *)
      echo "  error:not current oprator: $deplyType!";;
    esac
  fi
else
  sit;
fi